<template>
  <div
    v-if="trade"
    class="app-container"
  >
    <page-header
      title="异常交易详情"
      show-back
    >
      <div class="d-flex justify-content-between">
        <el-button
          v-for="(button,index) in trade.handles"
          :key="index"
          :type="button.type || 'primary'"
          size="small"
          @click="onAction(button)"
        >{{ button.text }}</el-button>
        <el-button
          v-if="trade.canRefund"
          type="danger"
          size="small"
          @click="onRefund(trade.id)"
        >退款</el-button>
      </div>
    </page-header>
    <el-container>
      <el-main class="py-0 pl-0">
        <el-card
          shadow="never"
          header="交易信息"
        >
          <em-descriptions
            :column="4"
            gutter="10px 0"
            size="small"
            label-position="right"
            label-width="100px"
            label-suffix="："
          >
            <em-descriptions-item label="交易ID">
              {{ trade.id }}
            </em-descriptions-item>
            <em-descriptions-item label="交易编号">
              {{ trade.trade_no }}
              <em-clipboard
                :content="trade.trade_no"
                icon="copy-layer"
                active-icon="em-icon-success-o"
                @success="clipboardSuccess"
              />
            </em-descriptions-item>
            <em-descriptions-item label="统一商户订单号">
              {{ trade.unified_no }}
              <em-clipboard
                :content="trade.unified_no"
                icon="copy-layer"
                active-icon="em-icon-success-o"
                @success="clipboardSuccess"
              />
            </em-descriptions-item>
            <em-descriptions-item label="商户订单号">
              {{ trade.bizno }}
              <em-clipboard
                :content="trade.bizno"
                icon="copy-layer"
                active-icon="em-icon-success-o"
                @success="clipboardSuccess"
              />
            </em-descriptions-item>
            <em-descriptions-item label="结算方式">
              <el-tag
                v-if="trade.balance"
                :type="trade.balance.class"
                size="mini"
              >{{ trade.balance.label }}</el-tag>
            </em-descriptions-item>
            <em-descriptions-item label="支付方式">
              <el-tag
                v-if="trade.paymode"
                :type="trade.paymode.class"
                size="mini"
              >{{ trade.paymode.label }}</el-tag>
            </em-descriptions-item>
            <em-descriptions-item label="交易总额">
              <em-currency v-model="trade.total" />
            </em-descriptions-item>
            <em-descriptions-item label="现金金额">
              <em-currency v-model="trade.cash" />
            </em-descriptions-item>
            <em-descriptions-item label="积分金额">
              <em-currency v-model="trade.credit" />
            </em-descriptions-item>
            <em-descriptions-item label="创建时间">
              {{ trade.created_at | asDatetime }}
            </em-descriptions-item>
            <em-descriptions-item label="更新时间">
              {{ trade.updated_at | asDatetime }}
            </em-descriptions-item>
            <em-descriptions-item label="交易状态">
              <el-tag
                :type="trade.status.class"
                size="mini"
              >{{ trade.status.label }}</el-tag>
            </em-descriptions-item>
            <em-descriptions-item label="是否异常交易">
              {{ trade.isAbnormal }}
            </em-descriptions-item>
            <em-descriptions-item label="是否可以退款">
              {{ trade.canRefund }}
            </em-descriptions-item>
          </em-descriptions>
        </el-card>

        <el-card
          v-for="(tradeOrder,index) in trade.orders"
          :key="index"
          shadow="never"
        >
          <template #header>
            <div class="d-flex justify-content-between">
              <span>交易支付信息</span>
              <div class="d-flex justify-content-between">
                <el-button
                  v-for="(button,bindex) in tradeOrder.handles"
                  :key="bindex"
                  :type="button.type || 'primary'"
                  size="small"
                  @click="onAction(button, tradeOrder.id)"
                >{{ button.text }}</el-button>
              </div>
            </div>
          </template>
          <em-descriptions
            :column="4"
            gutter="10px 0"
            size="small"
            label-position="right"
            label-width="100px"
            label-suffix="："
          >
            <em-descriptions-item label="支付方式">
              <el-tag
                v-if="tradeOrder.paymode"
                :type="tradeOrder.paymode.class"
                size="mini"
              >{{ tradeOrder.paymode.label }}</el-tag>
            </em-descriptions-item>
            <em-descriptions-item label="支付ID">
              {{ tradeOrder.id }}
            </em-descriptions-item>
            <em-descriptions-item label="支付单号">
              {{ tradeOrder.pay_no }}
              <em-clipboard
                v-if="tradeOrder.pay_no"
                :content="tradeOrder.pay_no"
                icon="copy-layer"
                active-icon="em-icon-success-o"
                @success="clipboardSuccess"
              />
            </em-descriptions-item>
            <em-descriptions-item label="支付金额">
              {{ tradeOrder.pay | asCurrency }}
            </em-descriptions-item>
            <em-descriptions-item label="支付时间">
              {{ tradeOrder.pay_time | asDatetime }}
            </em-descriptions-item>
            <em-descriptions-item label="结算方式">
              <el-tag
                v-if="tradeOrder.balance"
                :type="tradeOrder.balance.class"
                size="mini"
              >{{ tradeOrder.balance.label }}</el-tag>
            </em-descriptions-item>
            <em-descriptions-item label="创建时间">
              {{ tradeOrder.created_at | asDatetime }}
            </em-descriptions-item>
            <em-descriptions-item label="更新时间">
              {{ tradeOrder.updated_at | asDatetime }}
            </em-descriptions-item>
            <em-descriptions-item label="交易状态">
              <el-tag
                v-if="tradeOrder.status"
                :type="tradeOrder.status.class"
                size="mini"
              >{{ tradeOrder.status.label }}</el-tag>
            </em-descriptions-item>
          </em-descriptions>
        </el-card>
      </el-main>
      <el-aside width="300px">
        <el-card
          v-if="trade.user"
          shadow="never"
          header="下单用户"
        >
          <em-cell
            :title="trade.user.username"
            :label="`UID: ${trade.user.id}`"
            size="small"
            type="media"
            center
            class="p-0"
          >
            <el-avatar
              slot="icon"
              :src="trade.user.avatar"
              :size="36"
            />
          </em-cell>
        </el-card>
        <el-card
          v-if="trade.bizOrder"
          shadow="never"
          header="商户统一订单信息"
        >
          <em-descriptions
            :column="1"
            gutter="10px 0"
            size="small"
            label-position="right"
            label-width="100px"
            label-suffix="："
          >
            <em-descriptions-item label="ID">
              {{ trade.bizOrder.id }}
            </em-descriptions-item>
            <em-descriptions-item label="统一编号">
              {{ trade.bizOrder.order_no }}
              <em-clipboard
                :content="trade.bizOrder.order_no"
                icon="copy-layer"
                active-icon="em-icon-success-o"
                @success="clipboardSuccess"
              />
            </em-descriptions-item>
            <em-descriptions-item label="订单金额">
              {{ trade.bizOrder.amount }}
            </em-descriptions-item>
            <em-descriptions-item label="支付交易号">
              {{ trade.bizOrder.trade_no }}
            </em-descriptions-item>
            <em-descriptions-item label="应付金额">
              {{ trade.bizOrder.pay }}
            </em-descriptions-item>
            <em-descriptions-item label="实付积分">
              {{ trade.bizOrder.pay_time>0?trade.bizOrder.pay_credit:'--' }}
            </em-descriptions-item>
            <em-descriptions-item label="付款时间">
              {{ trade.bizOrder.pay_time | asDatetime }}
            </em-descriptions-item>
          </em-descriptions>
        </el-card>
        <el-card
          v-if="trade.logs"
          header="订单日志"
          shadow="never"
        >
          <el-timeline>
            <el-timeline-item
              v-for="(log, index) in trade.logs"
              :key="index"
              :icon="log.icon"
              :color="log.color"
              :size="log.size"
              :timestamp="log.created_at | asDatetime"
            >
              {{ log.explain }}
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-aside>
    </el-container>
  </div>
</template>

<script>
import PageHeader from '@/components/PageHeader'
import GridApiService from '@/api/grid'
export default {
  name: 'OrderView',
  components: {
    PageHeader
  },
  data() {
    return {
      apiService: new GridApiService('/trade/abnormal'),
      trade: null
    }
  },
  created() {
    this.onLoad()
  },
  methods: {
    onLoad() {
      const id = this.$route.params.id
      this.apiService.view(id).then(res => {
        this.trade = res.data
      })
    },
    clipboardSuccess() {
      this.$message({
        message: '已复制',
        type: 'success',
        duration: 1500
      })
    },
    onAction(button, tradeOrderId = 0) {
      console.log('点击了下载按钮', button)
      this.$confirm('请仔细核对异常交易，确认无误后再进行操作，确认操作该交易？', '交易操作提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(_ => {
          this.$http.trade
            .handle({
              handle: button.name,
              trade_id: this.trade.id,
              trade_order_id: tradeOrderId
            })
            .then(res => {
              this.onRefresh()
            })
        })
        .catch(_ => {})
    },
    onRefresh() {
      this.onLoad()
    },
    onRefund(tradeId) {
      this.$confirm(
        '请仔细核对异常交易，确认无误后再进行操作，确认要退款当前交易？',
        '交易退款提醒',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(_ => {
          this.$http.trade.refund(tradeId).then(res => {
            this.onRefresh()
          })
        })
        .catch(_ => {})
    }
  }
}
</script>
<style lang="scss" scoped>
.el-card {
  margin-bottom: 1em;
}
.console-action {
  display: flex;
  align-items: center;
  justify-content: space-between;
  &__end {
    display: flex;
    align-items: center;
  }
}
</style>
